A single item in the inventory is represented by a map, and the whole inventory is a list of such maps.
Implement the sort_by_price/1 function. It should take the inventory and return it sorted by item price, ascending.
After sorting your inventory by price, you noticed that you must have made a mistake when you were taking stock and forgot to fill out prices for a few items.
Implement the with_missing_price/1 function. It should take the inventory and return a list of items that do not have prices.
You noticed that some item names have a word that you don't like to use anymore. Now you need to update all the item names with that word.
Implement the update_names/3 function. It should take the inventory, the old word that you want to remove, and a new word that you want to use instead. It should return a list of items with updated names.
Some items were selling especially well, so you ordered more, in all sizes.
Implement the increase_quantity/2 function. It should take a single item and a number n, and return that item with the quantity for each size increased by n.
To know how much space you need in your storage, you need to know how many of each item you have in total.
Implement the total_quantity/1 function. It should take a single item and return how many pieces you have in total, in any size.
https://exercism.org/tracks/elixir/exercises/boutique-inventory
defmodule BoutiqueInventory do
@moduledoc """
practice Enum module
"""
@spec sort_by_price(inventory :: [any()]) :: [any()]
def sort_by_price(inventory), do: inventory |> Enum.sort_by(fn item -> item.price end, :asc)
@spec with_missing_price(inventory :: [any()]) :: [any()]
def with_missing_price(inventory), do: inventory |> Enum.filter(fn item -> !item.price end)
@spec update_names(inventory :: [any()], old_word :: String.t(), new_word :: String.t()) :: [any()]
def update_names(inventory, old_word, new_word) do
inventory
|> Enum.map(fn item ->
if String.contains?(item.name, old_word),
# do: Enum.into(%{name: String.replace(item.name, old_word, new_word)}, item),
do: %{item | name: String.replace(item.name, old_word, new_word)},
else: item
end)
end
@spec increase_quantity(item :: map(), count: Integer) :: map()
def increase_quantity(item, count) do
%{
quantity_by_size:
item.quantity_by_size
|> Enum.reduce(%{}, fn {key, value}, result ->
%{key => value + count} |> Enum.into(result)
end)
}
|> Enum.into(item)
end
@spec total_quantity(item :: map()) :: Integer
def total_quantity(item) do
item.quantity_by_size |> Enum.reduce(0, fn {_, value}, result -> result + value end)
end
end